package com.leyou.page.service.impl;

import com.leyou.common.utils.BeanHelper;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.dto.SpecGroupNameDTO;
import com.leyou.page.dto.SpecParamNameDTO;
import com.leyou.page.service.GoodsPageService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 虎哥
 */
@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    private final ItemClient itemClient;
    private final StringRedisTemplate redisTemplate;

    public GoodsPageServiceImpl(ItemClient itemClient, StringRedisTemplate redisTemplate) {
        this.itemClient = itemClient;
        this.redisTemplate = redisTemplate;
    }

    private static final String KEY_PREFIX_SPU = "page:spu:id:";
    private static final String KEY_PREFIX_SKU = "page:sku:id:";
    private static final String KEY_PREFIX_DETAIL = "page:detail:id:";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id:";
    private static final String KEY_PREFIX_BRAND = "page:brand:id:";
    private static final String KEY_PREFIX_SPEC = "page:spec:id:";

    @Override
    public String loadSpuData(Long spuId) {
        // 根据id查询spu
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        // 保留需要的数据
        Map<String,Object> map = new HashMap<>();
        map.put("id", spuDTO.getId());
        map.put("name", spuDTO.getName());
        map.put("brandId", spuDTO.getBrandId());
        map.put("categoryIds", spuDTO.getCategoryIds());
        // 序列化数据为json
        String json = JsonUtils.toJson(map);
        // 存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPU + spuId, json);
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        // 查询
        SpuDetailDTO detail = itemClient.querySpuDetailById(spuId);
        // 转JSON
        String json = JsonUtils.toJson(detail);
        // 存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_DETAIL + spuId, json);
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        // 查询
        List<SkuDTO> skuList = itemClient.querySkuBySpuId(spuId);
        // 转JSON
        String json = JsonUtils.toJson(skuList);
        // 存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SKU + spuId, json);
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        // 查询
        List<CategoryDTO> categoryDTOList = itemClient.queryCategoryByIds(ids);
        // 筛选需要的数据
        List<Map<String, Object>> list = new ArrayList<>();
        for (CategoryDTO categoryDTO : categoryDTOList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", categoryDTO.getId());
            map.put("name", categoryDTO.getName());
            list.add(map);
        }
        // 转JSON
        String json = JsonUtils.toJson(list);
        // 存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY + ids.get(ids.size() - 1), json);
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        // 查询
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        // 筛选需要的数据
        Map<String,Object> map = new HashMap<>();
        map.put("id", brandDTO.getId());
        map.put("name", brandDTO.getName());
        // 转JSON
        String json = JsonUtils.toJson(map);
        // 存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_BRAND + brandId, json);
        return json;
    }

    @Override
    public String loadSpecData(Long categoryId) {
        // 查询
        List<SpecGroupDTO> groupDTOList = itemClient.querySpecList(categoryId);
        // 筛选需要的数据
        List<SpecGroupNameDTO> list = new ArrayList<>();
        for (SpecGroupDTO groupDTO : groupDTOList) {
            // 创建规格组的对象
            SpecGroupNameDTO groupNameDTO = new SpecGroupNameDTO();
            // 填名字
            groupNameDTO.setName(groupDTO.getName());
            // 填params
            List<SpecParamDTO> params = groupDTO.getParams();
            List<SpecParamNameDTO> nameList = BeanHelper.copyWithCollection(params, SpecParamNameDTO.class);
            groupNameDTO.setParams(nameList);

            list.add(groupNameDTO);
        }
        // 转JSON
        String json = JsonUtils.toJson(list);
        // 存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPEC + categoryId, json);
        return json;
    }

    @Override
    public Boolean deleteSku(Long spuId) {
        return redisTemplate.delete(KEY_PREFIX_SKU + spuId);
    }
}
